package leeon.bbsbrower.views;

import leeon.bbsbrower.actions.BBSUserAction;
import leeon.bbsbrower.models.UserObject;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

public class UserListView extends ViewPart {
	public static final String ID = "bbsbrower.view.userlist";

	private Table table = null;
				
	//用户数据
	private static UserObject[] users = null;
	
	//入口标志，0 好友，1 所有友
	private static int en = 0;
	

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		
		table = new Table(parent, SWT.READ_ONLY|SWT.BORDER|SWT.FULL_SELECTION);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setFont(ApplicationWorkbenchWindowAdvisor.listFont);
		
		TableColumn col1 = new TableColumn(table, SWT.NONE);
		col1.setText("序号");
		col1.setWidth(50);
		
		TableColumn col2 = new TableColumn(table, SWT.NONE);
		col2.setText("友");
		col2.setWidth(50);
		
		TableColumn col3 = new TableColumn(table, SWT.NONE);
		col3.setText("代号");
		col3.setWidth(200);
		
		TableColumn col4 = new TableColumn(table, SWT.NONE);
		col4.setText("昵称");
		col4.setWidth(400);
		
		TableColumn col5 = new TableColumn(table, SWT.NONE);
		col5.setText("来自");
		col5.setWidth(200);
		
		TableColumn col6 = new TableColumn(table, SWT.NONE);
		col6.setText("状态");
		col6.setWidth(100);
		
				
		//上下左右的事件
		table.addKeyListener(new KeyListener() {

			public void keyPressed(KeyEvent e) {
				//左
				if (e.keyCode == SWT.ARROW_LEFT) {
					actionToFriend();					
					e.doit = false;
					
				//右
				} else if (e.keyCode == SWT.ARROW_RIGHT || e.keyCode == 13) {
					
					actionContent();
					e.doit = false;
				}
			
				
			}

			public void keyReleased(KeyEvent e) {}
			
		});
	}
	
	
	/**
	 * 回用户主菜单
	 */
	private void actionToFriend() {
		//跳转回到右键操作列表
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			FriendView frd = (FriendView)page.showView(FriendView.ID);
			if (UserListView.en == 0) frd.refreshData(2);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}

	
	/**
	 * 进入某个好友
	 */
	private void actionContent() {
		//根据文章列表看文章的action
		int u = table.getSelectionIndex();
		String id = users[u].getId();

		
		//跳转进邮件
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			UserConView con = (UserConView)page.showView(UserConView.ID);
			con.refreshData(u, id);
		} catch (PartInitException e) {
			e.printStackTrace();
		}

	}
	
	/**
	 * 刷新数据
	 * @param blocks	 
	 */
	public void refreshData(int en, int index) {
		//如果是-1则不赋值
		if (en != -1) UserListView.en = en;
		
		//提取数据		
		if (en != -1) {
			//好友
			try {
				if (en == 0) {
					UserListView.users = BBSUserAction.getAllFriends();			
				//所友
				} else {
					UserListView.users = BBSUserAction.getAllUsers();
				}
			} catch (Exception e) {
				MessageDialog.openError(this.getViewSite().getShell(), "错误信息", e.toString());
				e.printStackTrace();
			}
			
		}
		
		table.removeAll();
		if (UserListView.users != null) {
			for (int i = 0; i < UserListView.users.length; i ++) {
				TableItem it = new TableItem(table, SWT.NONE);
				it.setText(new String[] {
						UserListView.users[i].getNumber(),
						UserListView.users[i].isFriend()?"√":"", 
						UserListView.users[i].getId(),
						UserListView.users[i].getNick(),
						UserListView.users[i].getFrom(),
						UserListView.users[i].getStatus()
				});
				it.setData(UserListView.users[i]);
			}
			//选中某个
			table.setSelection(index);
		}
		
		ApplicationActionBarAdvisor.setStatusLine();
	}	
	
	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		table.setFocus();
	}
}